package cc.magicjson.example.demo.thead;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class PhAcidDetectionScheduler {

    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    // 日常检测 1.空白 2.标准
    public void startDailyTasks() {
        Runnable dailyTask = () -> {
            performBlankDetection();
            performStandardDetection();
        };

        // 安排每天执行一次任务，第一次立即执行，然后每24小时执行一次
        scheduler.scheduleAtFixedRate(dailyTask, 0, 24, TimeUnit.SECONDS);
    }

    public void performBlankDetection() {
        System.out.println("执行空白检测...");
        // 在此实现空白检测逻辑
    }

    public void performStandardDetection() {
        System.out.println("执行标准检测...");
        // 在此实现标准检测逻辑
    }

    // 开始酸度检测
    public void startIrregularPhDetection() {
        Runnable irregularTask = () -> {
            performPhDetection();
        };

        // 示例中通过随机时间间隔触发不定时任务，可以替换为实际触发机制
        new Thread(() -> {
            try {
                while (true) {
                    // 随机休眠一段时间以模拟不定时触发
                    Thread.sleep((long) (Math.random() * 10000));
                    irregularTask.run();
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }

    public void performPhDetection() {
        System.out.println("执行酸度检测...");
        // 在此实现酸度检测逻辑
    }

    public static void main(String[] args) {
        //
        PhAcidDetectionScheduler scheduler = new PhAcidDetectionScheduler();
        scheduler.startDailyTasks();
        scheduler.startIrregularPhDetection();

    }
}
